<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:48
 * base,login,index,Release
 */
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\View;
class Base extends Controller
{
    /**
     * 初始化监听登录状态
     */
    public function _initialize()
    {
        
        if(!session('?user_id') || !session('?user_phone')){
            //--登录的定位还得走这判断一下---start
            if(isset($_POST['city'])){
                if(!session('my_location_city_id')){
                    session('my_location_city_name', '北京市');
                    session('my_location_city_id', 1002);
                    $city = $_POST['city'];
                    if($city){
                        $id = Db::table('up_area')->where('district',$city)->where('level',2)
                            ->value('district_id');
                        if($id){
                            session('my_location_city_name', $city);
                            session('my_location_city_id', $id);
                        }
                    }
                }
            }
            $module     = strtolower(request()->module());
            $controller = strtolower(request()->controller());
            $action     = strtolower(request()->action());
            $url2        = $module."/".$controller;
            $url3       = $module."/".$controller."/".$action;
            $need_login_action = [
                strtolower('home/Release/newHouse')
                ,strtolower('home/news/index')
                ,strtolower('home/news/sysnews')
                ,strtolower('home/user')
            ];
            if(in_array($url2,$need_login_action) || in_array($url3,$need_login_action)){
                $this->redirect(url('home/login/indexLogin'));
            }
            //--登录的定位还得走这判断一下---end
            $jjrPhone = Db::table('up_about')->where('id',8)->value('content');
            $view = new View();
            $view::share("jjrPhone",$jjrPhone);
//            $this->redirect(url('home/login/indexLogin'));
        }else{
            $user = Db::table('up_u_user')
                ->where('user_id',session('user_id'))
                ->field('user_auth,user_auth_end,user_status,user_token')->find();
            $shop_status = $user['user_status'];
            if($shop_status == 3){
                $this->redirect(url('shop/login/indexLogin'));
            }
            //------------------------------------------------------
            if($user['user_auth'] > 1){
                $today = date('Y-m-d');
                if($user['user_auth_end'] < $today){
                    Db::table('up_u_user')->where('user_id',session('user_id'))->update(['user_auth'=>1]);
                }
            }
            //------------------------------------------------------
            $jjrPhone = Db::table('up_about')->where('id',8)->value('content');
            $view = new View();
            $view::share("jjrPhone",$jjrPhone);
        }
    }

    public function search($search, $page, $fl = 'search')
    {
        if ($search === null && $page === null) {
            session($fl, null);
        } elseif ($search === '') {
            session($fl, null);
        } elseif ($search !== null) {
            session($fl, serialize($search));
        }
        return unserialize(session($fl));
    }
    public function location()
    {
        if(!session('my_location_city_id')){
            session('my_location_city_name', '北京市');
            session('my_location_city_id', 1002);
            $city = $_POST['city'];
            if($city){
                $id = Db::table('up_area')->where('district',$city)->where('level',2)
                    ->value('district_id');
                if($id){
                    session('my_location_city_name', $city);
                    session('my_location_city_id', $id);
                    return json(['status'=>200,'msg'=>$city]);
                }
            }
            return json(['status'=>200,'msg'=>'北京市']);
        }else{
            return json(['status'=>200,'msg'=>session('my_location_city_name')]);
        }


    }
}